package com.zyd.blog.personal.service.impl;

import com.zyd.blog.personal.enuminterface.IsDelete;
import com.zyd.blog.personal.enuminterface.UserType;
import com.zyd.blog.persistence.mapper.UserCompanyMapper;
import com.zyd.blog.personal.service.UserCompanyService;
import com.zyd.blog.personal.service.UserInfoService;
import com.zyd.blog.personal.utils.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 用户公司信息的业务逻辑层
 * @author mfy
 */
@Service
public class UserCompanyServiceImpl implements UserCompanyService {

    @Resource
    private UserCompanyMapper userCompanyMapper;

    @Resource
    private UserInfoService userInfoService;

    @Override
    public Boolean isHaveUserCompany(Long userId){
        if(userId == null){
            return false;
        }
        Map<String,Object> condition = new HashMap<>(16);
        condition.put("isDelete", IsDelete.IS_DELETE_NO);
        condition.put("userId",userId);
        List<Map<String,Object>> company = userCompanyMapper.queryUserCompanyByCondition(condition);
        return !(company == null || company.size() == 0);
    }

    @Override
    public Map<String,Object> getDesignerInfo(Long userId){
        Map<String,Object> result = new HashMap<>(16);

        List<Map<String,Object>> userInfo = userInfoService.getUserInfo(userId);
        if(userInfo == null || userInfo.size() == 0){
            return new HashMap<>(0);
        }
        result.put("userId",userInfo.get(0).get("userId"));
        result.put("sex",userInfo.get(0).get("sex"));
        result.put("telephone",userInfo.get(0).get("telephone"));
        result.put("headPicture",userInfo.get(0).get("headPicture"));
        result.put("email",userInfo.get(0).get("email"));
        result.put("username",userInfo.get(0).get("username"));
        result.put("nickname",userInfo.get(0).get("nickname"));
        SimpleDateFormat year = new SimpleDateFormat("yyyy");
        String designTime = "designTime";
        if(0 == (Long)userInfo.get(0).get(designTime)){
            result.put("designTime",0);
        }else{
            result.put("designTime",year.format(new Date((Long) userInfo.get(0).get("designTime"))));
        }
        String userType = "userType";
        if(UserType.USER_TYPE_DESIGNER.equals(userInfo.get(0).get(userType))){
            result.put("userType","设计师");
            Map<String,Object> condition = new HashMap<>(16);
            condition.put("isDelete", IsDelete.IS_DELETE_NO);
            condition.put("userId",userId);
            List<Map<String,Object>> company = userCompanyMapper.queryUserCompanyByCondition(condition);
            if(company == null || company.size() == 0){
                return result;
            }else{
                result.put("companyName",company.get(0).get("companyName"));
                result.put("designType",company.get(0).get("duty"));
                result.put("teamName",company.get(0).get("teamName"));
                result.put("businessLicense",company.get(0).get("businessLicense"));
                result.put("teamDetail",company.get(0).get("teamDetail"));
                Map<String,Object> workAddress = StringUtils.splitAddress((String) company.get(0).get("workAddress"));
                result.put("workAddress",workAddress);
                String[] serviceAddress = ((String) company.get(0).get("serviceAddress")).split("#");
                result.put("serviceAddress",serviceAddress);
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
                result.put("settleTime",simpleDateFormat.format(new Date((Long) company.get(0).get("joinTime"))));
            }
        }else{
            result.put("userType","普通用户");
        }
        return result;
    }

    @Override
    public Boolean addUserCompanyInfo(Map<String,Object> map){
        Long userId = (Long) map.get("userId");
        if(userId == null){
            return false;
        }
        map.put("updateTime",(new Date()).getTime());
        map.put("addTime",(new Date()).getTime());
        return userCompanyMapper.addUserCompany(map) > 0;
    }

    @Override
    public Boolean updateCompany(Map<String,Object> map){
        Long id = (Long) map.get("id");
        Long userId = (Long) map.get("userId");
        if(id == null && userId == null){
            return false;
        }
        map.put("updateTime",(new Date()).getTime());
        return userCompanyMapper.updateUserCompany(map) > 0;
    }
}
